using System;
using System.Collections.Generic;
using System.Data;

using Elev.Core.Calculator;
using Elev.Core.DAL;
using Elev.Core.Utility;

namespace Elev.Core
{
    public class Grade : IDimension, ICalculable
    {
        int id;
        int examID;
        int studentID;
        double mark;
        string note;

        Exam exam;

        public int ID { get { return id; } }

        public int ExamID { get { return examID; } }

        public Exam Exam
        {
            get
            {
                if (examID > 0)
                {
                    if (exam == null)
                    {
                        ExamsFactory factory = new ExamsFactory();
                        exam = factory.GetExam(examID);
                    }
                    return exam;
                }
                return null;
            }
        }

        public int StudentID { get { return studentID; } }

        public Student Student
        {
            get
            {
                if (studentID > 0)
                {
                    StudentsFactory factory = new StudentsFactory();
                    return factory.GetStudent(studentID);
                }
                return null;
            }
        }

        public string Note
        {
            get { return note; }
            set { note = value; }
        }

        public Grade(DataRow row)
        {
            parse(row);
        }

        public Grade() : this(-1, -1, -1, -1, "") { }

        public Grade(int ExamID, int StudentID) : this(-1, ExamID, StudentID, -1, "") { }

        public Grade(int ID, int ExamID, int StudentID, double Mark, string Note)
        {
            id = ID;
            examID = ExamID;
            studentID = StudentID;
            mark = Mark;
            note = Note;
        }

        void parse(DataRow dr)
        {
            id = Convert.ToInt32(dr["ID"]);
            examID = Convert.ToInt32(dr["ExamID"]);
            studentID = Convert.ToInt32(dr["StudentID"]);
            mark = Convert.ToDouble(dr["Mark"]);
            note = (string)dr["Note"];
        }

        #region IDimension Members

        public int Save()
        {
            GradeInternal gradeInternal = new GradeInternal();
            if (id == -1)
            {
                // New creation ...
                id = gradeInternal.Add(examID, studentID, mark, note);
            }
            else
            {
                gradeInternal.Update(examID, studentID, mark, note);
            }

            return id;
        }

        public void Delete()
        {
            GradeInternal gradeInternal = new GradeInternal();
            gradeInternal.Delete(id);

            // Reset this instance
            id = -1;
            mark = -1;
            note = "";
        }

        #endregion

        public override string ToString()
        {
            if (mark == -1) return "";
            return mark.ToString();
        }

        #region ICalculable Members

        public double Mark
        {
            get { return mark; }
            set { mark = value; }
        }

        public int Coefficient
        {
            get
            {
                return Exam.Coefficient;
            }
        }

        #endregion
    }
}
